########################
#Jillienne Haglund and Ryan Welch

#Matching for "From Litigation to Rights"
#######################

#check if packages are installed, and if not, install them
packages <- c("foreign", "MatchIt", "optmatch", "rgenoud")
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
  install.packages(setdiff(packages, rownames(installed.packages())), repos="http://cran.rstudio.com/")  
}

#attach packages
lapply(packages, library, character.only = T)

#default the stringsAsFactors argument to FALSE for all functions with that argument
options(stringsAsFactors=FALSE)

#turn off scientific notation
options(scipen=999)

#set seed
set.seed(33603)

#set working directory
setwd("C:\\Users\\ryanm\\Dropbox\\Shared Research\\RegionalCourt_NHRIPaper\\Data")

#load data
d <- read.csv("ISQ_ReplicationData.csv", stringsAsFactors = F, strip.white = T)

#dichotomize numcase
d$numcase_di <- ifelse(d$numcase > 0, 1, 0)
d$numcase_di2 <- ifelse(d$numcase > mean(d$numcase, na.rm = T), 1, 0)

#Subset data by year and country
data <- d[d$year>=1980,]
data <- d[d$cowcode >= 200 & d$cowcode <= 395,]

#dichotomize civil society
data$civsoc_di <- ifelse(data$v2xcs_ccsi > mean(data$v2xcs_ccsi, na.rm = T), 1, 0)

#keep the variables we want for analysis
data <-  data[, c("cowcode", "year", "repressionlatent", "threeway", "numcasexcsoc", "nhrixcsoc", "numcasexnhri", "numcase", "hasnhri", "v2xcs_ccsi", "repressionlag", "lji", "xconst", "popmillionslog", "lgdp", "numcase_di", "numcase_di2", "civsoc_di")]


#listwise delete
data <- na.omit(data) 

#attach data
attach(data)

#########################################################
#Matching
########################################################

#Cases

#numcase2 (Hi values over mean)
#Nearest Neighbor (one to one)
nearest.case <- matchit(numcase_di2 ~ v2xcs_ccsi + lji + xconst + popmillionslog + lgdp, data = data, method ="nearest", distance = "logit", discard = "none", replace = T)

#Table 5: ECtHR Row
nearest.case
summary(nearest.case)

#create data frame
nearest.case.data<-match.data(nearest.case)

#Table 6: ECtHR Row
reg2 <- lm(repressionlatent ~ threeway + numcasexcsoc + nhrixcsoc + numcasexnhri + numcase + hasnhri + v2xcs_ccsi + repressionlag + lji + xconst + popmillionslog + lgdp, data = nearest.case.data)
summary(reg2)
##########################################################################

#NHRI

#Nearest Neighbor (one to one)
nearest.nhri <- matchit(hasnhri ~ v2xcs_ccsi + lji + xconst + popmillionslog + lgdp, data = data, method ="nearest", distance = "logit", discard = "none", replace = T)

#Table 5: NHRI Row
nearest.nhri
summary(nearest.nhri) 

nearest.nhri.data<-match.data(nearest.nhri)

#Table 6: NHRI Row
reg <- lm(repressionlatent ~ threeway + numcasexcsoc + nhrixcsoc + numcasexnhri + numcase + hasnhri + v2xcs_ccsi + repressionlag + lji + xconst + popmillionslog + lgdp, data = nearest.nhri.data)
summary(reg)
##############################################

#Civil Society
#Nearest Neighbor (one to one)
nearest.cs <- matchit(civsoc_di ~ lji + xconst + popmillionslog + lgdp + hasnhri, data = data, method ="nearest", distance = "logit", discard = "none", reestimate = TRUE, replace = T)

#Table 5: CivSoc Row
nearest.cs
summary(nearest.cs)

#create data frame
nearest.cs.data<-match.data(nearest.cs)

#Table 6: CivSoc Row
reg3 <- lm(repressionlatent ~ threeway + numcasexcsoc + nhrixcsoc + numcasexnhri + numcase + hasnhri + v2xcs_ccsi + repressionlag + lji + xconst + popmillionslog + lgdp, data = nearest.cs.data)
summary(reg3)
